<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>商品管理</title>
    <link rel="stylesheet" href="./bootstrap.css">
    <script src="./vue.js"></script>
</head>

<body>
    <div id="app">
        <!-- 创建表头 -->
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">商品管理列表</h3>
            </div>
            <div class="panel-body form-inline">
                <label>id:</label>
                <input type="text" class="form-control" v-model="pID">
                <label>name:</label>
                <input type="text" class="form-control" v-model="pName">
                <input class="btn btn-primary" type="button" value="添加" @click="add">
                <input type="text" class="form-control" placeholder="关键字查询" v-model="keywords">
            </div>
        </div>
        <!-- 创建表 -->
        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>id</th>
                    <th>name</th>
                    <th>time</th>
                    <th>option</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item, index) in search()" :key="item.id">
                    <td>{{ item.id }}</td>
                    <td>{{ item.name }}</td>
                    <td>{{ item.ctime }}</td>
                    <td>
                        <a href="" @click.prevent="del(item.id)">删除</a>
                    </td>
                </tr>
            </tbody>

        </table>
    </div>
</body>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            pID: '',
            pName: '',
            keywords: '',
            productList: [{
                    id: 1,
                    name: '宝马',
                    ctime: new Date()
                },
                {
                    id: 2,
                    name: '奔驰',
                    ctime: new Date()
                },
            ]
        },
        methods: {
            add() {
                this.productList.push({
                    id: this.pID,
                    name: this.pName,
                    ctime: new Date()
                })
            },
            del(id) {
                // console.log('删除')
                // 删除是根据 商品的id 进行删除
                // 1. 先要找到对应的id 商品
                // 2. 然后从数组中对其进行删除操作

                // ES6 some()
                // this.productList.some((item,i) => {
                //     console.log(item,i)
                //     if (item.id == id) {
                //         // ES6 splice()
                //         this.productList.splice(i,1)
                //     }
                // })

                // ES6 findIndex
                var index = this.productList.findIndex(item => {
                    if (item.id == id) {
                        return true
                    }
                })

                this.productList.splice(index, 1)
            },
            search() { // 根据关键字，进行数据搜索
                /*
                var newList = []
                this.productList.forEach(item => {
                    if (item.name.indexOf(this.keywords) != -1) {
                        newList.push(item)
                    }
                });
                return newList
                */
                // forEach/some/findIndex/indexOf/filter
                return this.productList.filter((item) => {
                    // 
                    if (item.name.includes(this.keywords)) {
                        return item
                    }
                })
            }
        },

    })
</script>

</html>